package com.yxq.mdt.app.modules.auction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.orhanobut.hawk.Hawk;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.application.App;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.auction.bean.HonorValueBean;
import com.yxq.mdt.app.modules.auction.model.AuctionModel;
import com.yxq.mdt.app.modules.main.bean.NameAndPhoneBean;
import com.yxq.mdt.app.modules.pm.ui.GoodsDetailTabActivity;
import com.yxq.mdt.app.modules.pm.ui.PmGoodsDetailTabVerticalActivity;
import com.yxq.mdt.app.modules.webview.ui.MiWebViewActivity;
import com.yxq.mdt.app.net.API;
import com.yxq.mdt.app.net.ApiModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 成为团长
 */
public class BecomeTeamAc extends BaseActivity {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.become_go_give)
    TextView becomeGoGive;
    @BindView(R.id.become_like_tame)
    TextView becomeLikeTame;
    @BindView(R.id.become_money)
    TextView becomeMoney;
    @BindView(R.id.become_check_box)
    CheckBox becomeCheckBox;
    @BindView(R.id.become_check_agreement)
    TextView becomeCheckAgreement;
    double isMoney;
    @BindView(R.id.become_like_tame_quick)
    TextView becomeLikeTameQuick;

    @Override
    public int getLayoutId() {
        return R.layout.activity_become_team;
    }

    @Override
    public void initView() {
        becomeLikeTame.setEnabled(true);
        App.getInstance().addActivity(this);
        isShowTeam();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    protected void back() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_OK);
        finish();
    }

    /**
     * 申请状态显示
     */
    private void isShowTeam() {
        AuctionModel.getHonorValue(new ApiModel.CommonCallback<HonorValueBean>() {
            @Override
            public void onSuccess(HonorValueBean honorValueBean) {
                if (honorValueBean != null) {
                    isMoney = honorValueBean.getPayCount();
                    becomeMoney.setText(String.format("您当前累计消费：%.2f元", honorValueBean.getPayCount()));
                    switch (honorValueBean.getState()) {
                        case -1:
                            becomeCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                                if (!isChecked) {
                                    becomeLikeTame.setBackgroundResource(R.drawable.shape_btn_lin_selector);
                                    becomeLikeTame.setTextColor(getResources().getColor(R.color.color_font_black));
                                    becomeLikeTame.setEnabled(true);
                                } else {
                                    becomeLikeTame.setBackgroundResource(R.drawable.shape_btn_yellow_selector);
                                    becomeLikeTame.setTextColor(getResources().getColor(R.color.white));
                                    becomeLikeTame.setEnabled(true);
                                }
                            });
                            becomeLikeTame.setText("普通申请为团长");
                            break;
                        case 0:
                            becomeLikeTame.setText("等待审核");
                            becomeLikeTame.setBackgroundResource(R.drawable.shape_btn_lin_selector);
                            becomeLikeTame.setTextColor(getResources().getColor(R.color.gray_pressed));
                            becomeLikeTame.setEnabled(true);
                            becomeCheckBox.setEnabled(false);
                            becomeCheckBox.setChecked(true);
                            becomeLikeTameQuick.setVisibility(View.GONE);
                            break;
                        case 1:
                            becomeLikeTame.setText("审核通过");
                            becomeLikeTame.setBackgroundResource(R.drawable.shape_btn_lin_selector);
                            becomeLikeTame.setTextColor(getResources().getColor(R.color.gray_pressed));
                            becomeLikeTame.setEnabled(true);
                            becomeCheckBox.setChecked(true);
                            becomeCheckBox.setEnabled(false);
                            becomeLikeTameQuick.setVisibility(View.GONE);
                            break;
                        case 2:
                            becomeLikeTame.setText("审核不通过");
                            becomeLikeTame.setBackgroundResource(R.drawable.shape_btn_lin_selector);
                            becomeLikeTame.setTextColor(getResources().getColor(R.color.gray_pressed));
                            becomeLikeTame.setEnabled(true);
                            becomeCheckBox.setChecked(true);
                            becomeCheckBox.setEnabled(false);
                            becomeLikeTameQuick.setVisibility(View.GONE);
                            break;
                    }
                }
            }

            @Override
            public void onError(String errMsg) {
                showBottomToast(errMsg);
            }
        });

    }

    @OnClick({R.id.become_go_give, R.id.become_like_tame, R.id.become_check_agreement, R.id.become_like_tame_quick})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.become_like_tame_quick:
            case R.id.become_go_give:
                //购买大礼包
                NameAndPhoneBean nameAndPhoneBean = Hawk.get(SPKey.SP_KEY_NAME_PHONE_INFO);
                Intent it = new Intent(mActivity, PmGoodsDetailTabVerticalActivity.class);
                it.putExtra("id", nameAndPhoneBean.getPmSetting().getGift_goods_id() + "");
                startActivity(it);
                break;
            case R.id.become_like_tame:
                //申请成为团长
                if (!becomeCheckBox.isChecked()) {
                    showBottomToast("请先勾选《团长协议》");

                } else {
                    if (isMoney < 7980d) {
                        showBottomToast("累计消费达7980,才能申请成为团长");
                    } else {
                        AuctionModel.addPmUserGrade("0", "1", new ApiModel.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String s) {
                                becomeLikeTame.setBackgroundResource(R.drawable.shape_btn_lin_selector);
                                becomeLikeTame.setTextColor(getResources().getColor(R.color.gray_pressed));
                                becomeLikeTame.setText("等待审核");
                                becomeLikeTame.setEnabled(true);
                                becomeCheckBox.setChecked(true);
                                becomeCheckBox.setEnabled(false);
                            }

                            @Override
                            public void onError(String errMsg) {
                                showBottomToast(errMsg);
                            }
                        });
                    }

                }
                break;
            case R.id.become_check_agreement:
                //查看团长协议
                it = new Intent(mActivity, MiWebViewActivity.class);
                it.putExtra("url", API.TEAM_WITHDRAWAL);
                startActivity(it);
                break;
        }
    }
}
